﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Intellego.Web.ValidationEngine;

namespace MIAPWEB.Forms.Controles
{
    public partial class wucVariablesPaquete : UserControl
    {
        public List<string> ListaVariables
        {
            get
            {
                if (ViewState["ListaVariables"] == null)
                {
                    ViewState["ListaVariables"] = new List<string>();
                }

                return (List<string>)ViewState["ListaVariables"];
            }
            set { ViewState["ListaVariables"] = value; }
        }

        private Dictionary<string,string> ValoresVariables
        {
            get
            {
                return (Dictionary<string, string>)ViewState["ValoresVariables"];
            }
            set { ViewState["ValoresVariables"] = value; }
        }


        protected override void LoadViewState(object savedState)
        {            
            base.LoadViewState(savedState);
            this.AddControls();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AddControls();
            }
            this.InitValidation();
        }

        /// <summary>
        /// Establece los parametros obligatorios en la captura
        /// </summary>
        private void InitValidation()
        {
            var errNotEmpty = this.GetGlobalResourceObject("Strings", "ErrorRequerido").ToString();
            var reglas= new List<ValidationRule>();

            foreach (Control ctl in DynamicControlsHolder.Controls)
            {
                if (!(ctl is TextBox)) continue;
                var txt = ctl as TextBox;
                var ruleEmpty = new ValidationRule(txt);
                ruleEmpty.NotEmpty(errNotEmpty);
                reglas.Add(ruleEmpty);
            }

            var masterPage = this.Page.Master;

            if (masterPage != null)
            {
                var parent = masterPage.Page as PageBase;

                if (parent != null)
                {
                    foreach (var validationRule in reglas)
                    {
                        parent.ValidationEngine.Add(validationRule);
                    }
                    
                }
            }
        }

        /// <summary>
        /// agrega los controles al sistema
        /// </summary>
        private void AddControls()
        {
            foreach (var nombreVar in ListaVariables)
            {                
                var lbl = new Label { ID = "lbl" + nombreVar,Text = nombreVar+@" :"};
                var txt = new TextBox { ID = "txt" + nombreVar };
                if (this.ValoresVariables != null)
                {
                    foreach (var variablesValor in
                        this.ValoresVariables.Where(variablesValor => variablesValor.Key.ToLower().Contains(nombreVar.ToLower())))
                    {
                        txt.Text = variablesValor.Value;
                    }                    
                }
                DynamicControlsHolder.Controls.Add(lbl);
                DynamicControlsHolder.Controls.Add(txt);
                DynamicControlsHolder.Controls.Add(new LiteralControl("<hr>"));
            }
        }

        /// <summary>
        /// Agrega los controles a la pantalla segun las variables
        /// </summary>
        /// <param name="listaVariables"></param>
        /// <param name="variablesValores"></param>
        public void AdddControls(List<string> listaVariables,Dictionary<string,string> variablesValores=null)
        {
            this.ListaVariables = listaVariables;
            this.DynamicControlsHolder.Controls.Clear();
            this.ValoresVariables = variablesValores;
            this.AddControls(); 
        }

        /// <summary>
        /// obtiene los valores de los textBox
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetValores()
        {

            var regreso= new Dictionary<string, string>();
            foreach (Control ctl in DynamicControlsHolder.Controls)
            {
                if (!(ctl is TextBox)) continue;
                var txt = ctl as TextBox;
                regreso.Add(txt.ID.Replace("txt",string.Empty),txt.Text);
            }

            return regreso;
        }
        
    }
}